package com.jgp.sys.query;


import com.jgp.common.persistence.Model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 项目   jgp
 * 作者   loufei
 * 时间   2017/10/19
 */
public class QueryFilterList extends ArrayList<QueryFilter> {
    private Class<? extends Model> joinClass;
    
    public QueryFilterList addFilter(String name, String operator) {
        this.add(new QueryFilter(name, operator));
        return this;
    }
    
    public QueryFilterList addFilter(String name, Operator operator) {
        this.add(new QueryFilter(name, operator));
        return this;
    }
    
    public QueryFilterList addFilter(String name, String operator, Object value) {
        this.add(new QueryFilter(name, operator, value));
        return this;
    }
    
    public QueryFilterList addFilter(String name, Operator operator, Object value) {
        this.add(new QueryFilter(name, operator, value));
        return this;
    }
    
    public QueryFilterList addFilters(QueryFilterList filters) {
        if (Objects.nonNull(filters)) this.addAll(filters);
        return this;
    }
    
    public QueryFilterList addFilters(List<QueryFilter> filters) {
        if (Objects.nonNull(filters)) this.addAll(filters);
        return this;
    }
    
    public Class<? extends Model> getJoinClass() {
        return joinClass;
    }
    
    public QueryFilterList setJoinClass(Class<? extends Model> joinClass) {
        this.joinClass = joinClass;
        return this;
    }
    
    public QueryFilterList extFilters(String prefix) {
        QueryFilterList filters = new QueryFilterList();
        filters.addFilters(this.stream().filter(q -> q.getName().contains(prefix + ".")).map(q -> {
            QueryFilter query = new QueryFilter();
            query.setName(q.getName().replace(prefix + ".", ""));
            query.setOperator(q.getOperator());
            query.setValue(q.getValue());
            return query;
        }).collect(Collectors.toList()));
        return filters;
    }
}
